package com.nexgo.payment.pack;

import com.nexgo.common.ByteUtils;
import com.nexgo.data.entity.SendData;
import com.nexgo.data.entity.SendDataBuilder;
import com.nexgo.data.entity.mapper.EntityDataMapper;
import com.nexgo.data.json.JsonSendData;
import com.nexgo.data.json.JsonTools;
import com.nexgo.data.json.bean.JsonBody;
import com.nexgo.data.json.bean.SendBodyData;
import com.nexgo.data.json.bean.SendHead;
import com.nexgo.iso8583.exception.WriteException;
import com.nexgo.oaf.apiv3.DeviceInfo;
import com.nexgo.payment.api.ApiDeviceEngine;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.entity.MessageData;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.util.MessageHelper;
import com.nexgo.payment.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 签到
 * <p>
 * Created by xiaox on 2017/7/10.
 */
class LoginBuilder extends DataBuilder {


    LoginBuilder(TransData transData, DataSource repository) {
        super(transData, repository);
    }

    @Override
    public SendData form() {
        //xhj add
        SendData sendData = new SendDataBuilder().setListener(mListener).setSignListener(mSignListener).createSendData();
        sendData.setHeader(MessageHelper.formMessageHeader(mRepository.getParamValue(PreferencesConstants.TPDU)));
        sendData.setMsgType(mTransData.getTransType().getMsgType());
        sendData.setProcCode(mTransData.getTransType().getProcCode());
        sendData.setTraceNum(mRepository.getParamValue(PreferencesConstants.TRACE_NUMBER));
        setCurruntTraceNumber(mRepository.getParamValue(PreferencesConstants.TRACE_NUMBER));
        mRepository.increaseTraceNumber();
//        wzp add
        sendData.setConditionCode(mTransData.getTransType().getConditionCode());
        sendData.setTerminalId(mRepository.getParamValue(PreferencesConstants.TERMINAL_ID));
        sendData.setMerchantId(mRepository.getParamValue(PreferencesConstants.MERCHANT_ID));
        sendData.setField60(formField60());
        sendData.setMac("0000000000000000");
//        sendData.setField62(formField62());
//        sendData.setField63(formField63());
        return sendData;
    }

    @Override
    public MessageData formJsonData() {
        MessageData sSendData = new MessageData();
        JsonSendData jsonSendData = new JsonSendData();
        SendHead head = new SendHead();
        JsonBody jsonBody = new JsonBody();

        //head
        head.setTRAN_PROCESS("");
        head.setTRAN_ID("");
        head.setTranscode("CIPP050001");
        String date = new SimpleDateFormat("yyyyMMddHHmmss", SysConstant.LOCALE).format(new Date());
        head.setTerm_trans_time(date);
        head.setMcht_id(mRepository.getParamValue(PreferencesConstants.MERCHANT_ID));
        //经纬度，签名暂时为空
        head.setLngx(mRepository.getParamValue(PreferencesConstants.LONGITUDE));
        head.setLngy(mRepository.getParamValue(PreferencesConstants.LATITUDE));
        head.setSign("0000000000000000");
        head.setVersion("1.0.0");
//        head.setGzip("0");
        jsonSendData.setREQ_HEAD(head);

        //body

        SendData cn8583Data = form();
        sSendData.setSendData(cn8583Data);

        final EntityDataMapper mapper = new EntityDataMapper();
        String cn8583Str = null;
        try {
            cn8583Str = ByteUtils.byteArray2HexString(mapper.transform(cn8583Data));
        } catch (WriteException e) {
            e.printStackTrace();
        }
        jsonBody.set_$8583data(cn8583Str);
        jsonBody.setTerm_id(mRepository.getParamValue(PreferencesConstants.TERMINAL_ID));
        jsonBody.setTerm_batch_no(mRepository.getParamValue(PreferencesConstants.BATCH_NUMBER));
        jsonBody.setTerm_pos_no(getCurruntTraceNumber());

        SendBodyData sendBodyData = new SendBodyData();

        String str = JsonTools.toJson(jsonBody);

        sendBodyData.setDECRYPT(str);

        jsonSendData.setREQ_BODY(sendBodyData);

        sSendData.setJsonSendData(jsonSendData);

        setScan(false);

        return sSendData;
    }

    private String formField60() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format(SysConstant.LOCALE, "%02d",
                Integer.parseInt(StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.CURRENT_OPERATOR_NO), "1"))));
        sb.append("0000");
        return ByteUtils.byteArray2HexString(sb.toString().getBytes());
    }

    private String formField62() {
        DeviceInfo deviceInfo = ApiDeviceEngine.getInstance().getDeviceInfo();
        StringBuilder sb = new StringBuilder();
        sb.append("Sequence No");
        String str;
        switch (deviceInfo.getModel().toUpperCase()) {
            case "N5":
                str = "9615";
                break;
            case "G870":
                str = "9542";
                break;
            default:
                str = "0000";
                break;
        }
        str += deviceInfo.getSn();
        sb.append(String.format(SysConstant.LOCALE, "%02d", str.length()));
        sb.append(str);
        return ByteUtils.byteArray2HexString(sb.toString().getBytes());
    }

    private String formField63() {
        return String.format(SysConstant.LOCALE, "%02d ", Integer.parseInt(StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.CURRENT_OPERATOR_NO), "1")));
    }
}
